package com.freedom.controller.w.core;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;

import com.freedom.vo.w.core.WebBaseVo;

public abstract class BaseWebPaginationCRUDController<VO extends WebBaseVo> extends WebCRUDBaseController{
	
	@Autowired
	protected VO vo;

	public VO getVo() {
		return vo;
	}

	public void setVo(VO vo) {
		this.vo = vo;
	}

	/**
	 * 使用vo的获取参数并分页
	 * @param param
	 * @return
	 */
	protected Map<String, Object> initPaginationParam(
			MultiValueMap<String, String> param) {
		if (param == null || param.isEmpty()) {
			return null;
		} else {
			Map<String, Object> result = new HashMap<String, Object>();
			for (String key : param.keySet()) {
				result.put(key, param.getFirst(key));
			}
			if (param.containsKey("page") && param.containsKey("rows")) {
				result.put("page",
						(Integer.parseInt(param.getFirst("page")) - 1)
								* Integer.parseInt(param.getFirst("rows")));
			}
			return result;
		}
	}
	
	/**
	 * 不使用vo的方式只初始化获取参数
	 * @param param
	 * @return
	 */
	protected Map<String, Object> initParam(
			MultiValueMap<String, String> param) {
		if (param == null || param.isEmpty()) {
			return null;
		} else {
			Map<String, Object> result = new HashMap<String, Object>();
			for (String key : param.keySet()) {
				result.put(key, param.getFirst(key));
			}
			return result;
		}
	}
}
